#include "registerwidget.h"
#include "ui_registerwidget.h"
#include <logonwidget.h>

RegisterWidget::RegisterWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWidget)
{
    ui->setupUi(this);
}

RegisterWidget::~RegisterWidget()
{
    delete ui;
}



void RegisterWidget::on_btn_register_clicked()
{
    //get register information
    QString regUsername = ui->reg_username_lineEdit->text();
    QString regPassword = ui->reg_password_lineEdit->text();
    QString regPasswordConfirm = ui->reg_password_conf_lineEdit->text();

    if(regPassword == regPasswordConfirm)
    {
        //connect to server
        QNetworkRequest request(QUrl("http://localhost:19999/slimarveous/register"));
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
        QJsonObject json;
        json.insert("username", regUsername);
        json.insert("password", regPassword);
        QJsonDocument doc(json);

        QByteArray data = doc.toJson(QJsonDocument::Compact);
        QNetworkReply *reply = manager.post(request, data);
        connect(reply, &QNetworkReply::finished, [=]() {
            if (reply->error() == QNetworkReply::NoError) {
                    // 读取响应数据
                    QByteArray dataReply = reply->readAll();

                    // 将 JSON 数据转换为 QVariant 对象
                    QJsonParseError error;
                    QJsonObject result = QJsonDocument::fromJson(dataReply, &error).object();
                    // 处理 JSON 数据
                    if (error.error == QJsonParseError::NoError) {
                        QVariantMap resultMap = result.toVariantMap();
                        //提取状态码
                        QVariant status_code = resultMap.value("status_code");
                        status_code.toString();

                        //判断注册是否有效
                        //注册有效
                        if(status_code == "2"){
                            QMessageBox::information(this,"success","恭喜你，注册成功！");
                            this->close();
                            //跳转回登录界面
                            LogOnWidget* w = new LogOnWidget();
                            w->show();
                        }
                        //冲突的用户名
                        else if(status_code == "1") {
                            QMessageBox::information(this,"failed","该用户名已被使用！");
                        }
                        //服务器出问题
                        else {
                            QMessageBox::information(this,"failed","好像哪里出了问题呢？肯定不是服务器的问题！");
                        }

                    }

            }
            else {
                qDebug()<<"error";
            }

                // 释放资源
                reply->deleteLater();

        });
    }
    else if(regPassword != regPasswordConfirm) {
        QMessageBox::information(this,"failed","两次输入的密码不一致！");
    }
}


void RegisterWidget::on_btn_return_clicked()
{
    this->close();
    LogOnWidget* w1 = new LogOnWidget();
    w1->show();
}

